/* 
Scenic Graphics Library
Copyright (C) 2007 Jouni Tulkki

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USApackage scenic;
*/

#include "Debug.h"
#include "scenic_Tesselator.h"
#include "Tesselator.h"


using namespace Scenic::Renderer;


class JavaTesselator : public Tesselator
{
	JNIEnv *env;
	jobject obj;
	jclass clazz;
	jmethodID addVertexMethod;
	jmethodID addTriangleMethod;
public:
	JavaTesselator(JNIEnv *env, jobject obj);
	~JavaTesselator();

	void addVertex(double x, double y);
	void addTriangle(int vertex1, bool edge1, int vertex2, bool edge2, int vertex3, bool edge3);
};

JavaTesselator::JavaTesselator(JNIEnv *env, jobject obj)
{
	this->env = env;
	this->obj = env->NewGlobalRef(obj);
	clazz = env->GetObjectClass(this->obj);
	addVertexMethod = env->GetMethodID(clazz, "addVertex", "(DD)V");
	addTriangleMethod = env->GetMethodID(clazz, "addTriangle", "(IZIZIZ)V");
}

JavaTesselator::~JavaTesselator()
{
	env->DeleteGlobalRef(obj);
}

void JavaTesselator::addVertex(double x, double y)
{
	env->CallVoidMethod(obj, addVertexMethod, x, y);
}

void JavaTesselator::addTriangle(int vertex1, bool edge1, int vertex2, bool edge2, int vertex3, bool edge3)
{
	env->CallVoidMethod(obj, addTriangleMethod, 
		vertex1, edge1 ? JNI_TRUE : JNI_FALSE,
		vertex2, edge2 ? JNI_TRUE : JNI_FALSE,
		vertex3, edge3 ? JNI_TRUE : JNI_FALSE);
}

JNIEXPORT jint JNICALL Java_scenic_Tesselator_begin
  (JNIEnv *env, jobject obj)
{
	ENTER(java_scenic_Tesselator_begin);

	Tesselator *t = new JavaTesselator(env, obj);

	t->begin();

	return t->id;
}

JNIEXPORT void JNICALL Java_scenic_Tesselator_end
  (JNIEnv *, jobject, jint id)
{
	ENTER(java_scenic_Tesselator_end);

	Tesselator *t = Tesselator::get(id);

	t->end();
	delete t;
}

JNIEXPORT void JNICALL Java_scenic_Tesselator_beginContour
  (JNIEnv *, jobject, jint id)
{
	ENTER(java_scenic_Tesselator_beginContour);

	Tesselator *t = Tesselator::get(id);

	t->beginContour();
}

JNIEXPORT void JNICALL Java_scenic_Tesselator_endContour
  (JNIEnv *, jobject, jint id)
{
	ENTER(java_scenic_Tesselator_endContour);

	Tesselator *t = Tesselator::get(id);

	t->endContour();
}

JNIEXPORT void JNICALL Java_scenic_Tesselator_vertex
  (JNIEnv *, jobject, jint id, jdouble x, jdouble y)
{
	ENTER(java_scenic_Tesselator_vertex);

	Tesselator *t = Tesselator::get(id);

	t->vertex(x, y);
}
